// pages/publish/publish.js
const http = require('../../utils/http.js');
const util = require('../../utils/util.js');
import regeneratorRuntime from '../../utils/runtime.js'
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: '1',
    show: false,
    checked: true,
    carpool: {
      "type": '1',
      "leaveTime": "",
      "leaveAddress": "",
      "arriveAddress": "",
      "remark": "",
      "person": '',
      "price": 0,
      "phone": "",
      "contactName": ""
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  onChange(event) {
    this.data.carpool.type = event.detail
    this.setData({
      carpool: this.data.carpool
    });
  },
  showPopup() {
    this.setData({
      show: true
    });
  },
  onType(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    // Toast(`当前值：${value}, 当前索引：${index}`);
    this.setData({
      show: false,
      type: value
    });
  },
  showTime() {
    this.setData({
      showTime: true
    });
  },
  hideTime() {
    this.setData({
      showTime: false
    });
  },
  // onTime(event) {
  //   const {
  //     picker,
  //     value,
  //     index
  //   } = event.detail;
  //   // Toast(`当前值：${value}, 当前索引：${index}`);
  //   this.setData({
  //     showTime: false,
  //     departure: value
  //   });
  // },
  onHome() {
    this.setData({
      checked: !this.data.checked
    });
  },
  come(event) {
    this.data.carpool.leaveAddress = event.detail
    this.setData({
      carpool: this.data.carpool
    });
  },
  go(event) {
    this.data.carpool.arriveAddress = event.detail
    this.setData({
      carpool: this.data.carpool
    });
  },
  onInput(event) {
    console.log(event.detail.value);
    var datetime = event.detail.value;
    this.data.carpool.leaveTime = datetime
    this.setData({
      carpool: this.data.carpool,
    });
  },
  contactName(event) {
    this.data.carpool.contactName = event.detail
    this.setData({
      carpool: this.data.carpool
    });
  },
  person(event) {
    this.data.carpool.person = event.detail
    this.setData({
      carpool: this.data.carpool
    });
  },
  phone(event) {
    this.data.carpool.phone = event.detail
    this.setData({
      carpool: this.data.carpool
    });
  },
  remark(event) {
    this.data.carpool.remark = event.detail
    this.setData({
      carpool: this.data.carpool
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    /* 建议放在onReady里执行，提前加载广告 */


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    // that.resetDate();
    var url = getCurrentPages()[0].route;
    var userInfo = app.globalData.userInfo;
    if (userInfo == null) {
      wx.showModal({
        title: '提示',
        content: '该操作需要登录，是否登录？',
        success(res) {
          if (res.confirm) {
            wx.setStorage({
              key: 'lastpage',
              data: url,
            })
            wx.redirectTo({
              url: '/pages/wode/login/login',
            })
          } else {
            wx.switchTab({
              url: '/pages/home/index',
            })
          }
        }
      })

    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },
  submit: async function() {
    var that = this;
    if (that.data.carpool.leaveAddress == "") {
      wx.showToast({
        title: '请填写出发地点',
        icon: 'none'
      })
      return;
    }
    if (that.data.carpool.arriveAddress == "") {
      wx.showToast({
        title: '请填写到达地点',
        icon: 'none'
      })
      return;
    }
    if (that.data.carpool.leaveTime == "") {
      wx.showToast({
        title: '请选择出发日期',
        icon: 'none'
      })
      return;
    }
    if (that.data.carpool.contactName == "") {
      wx.showToast({
        title: '请填写时间补充',
        icon: 'none'
      })
      return;
    }
    if (that.data.carpool.person == "") {
      wx.showToast({
        title: '请输入人数或空位',
        icon: 'none'
      })
      return;
    }
    if (!(/(^[1-9]\d*$)/.test(that.data.carpool.person))) {　　　　　　
      wx.showToast({
        title: '人数或空位请输入数字',
        icon: 'none'
      })
      return;　　
    }
    if (that.data.carpool.phone == "") {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return;
    }
    if (that.data.carpool.remark == "") {
      wx.showToast({
        title: '请填写描述补充',
        icon: 'none'
      })
      return;
    }
    wx.showModal({
      title: '提示',
      content: '请确认你填写的内容无误',
      success: async(e) => {
        if (e.confirm) {
          wx.showLoading({
            title: '正在提交..',
          })
          var res = await http.post('/carpool/add', that.data.carpool);
          if (res.status) {
            wx.showToast({
              title: '发布成功',
            })
            that.resetDate();

          } else {
            wx.showToast({
              title: '发布失败',
              icon: 'none'
            })
          }
        }
      }
    })

  },
  resetDate() {
    var info = {
      "type": '1',
      "leaveTime": "",
      "leaveAddress": "",
      "arriveAddress": "",
      "remark": "",
      "person": '',
      "price": 0,
      "phone": "",
      "contactName": ""
    };
    this.setData({
      carpool: info
    })
  }
})